package com.molichuxing.services.infrastructure.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.StockRecordBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.StockRecordMapper;
import com.molichuxing.services.infrastructure.dto.request.create.StockRecordCreateDto;
import com.molichuxing.services.infrastructure.dto.response.StockRecordDto;
import com.molichuxing.services.infrastructure.service.StockRecordService;

/**
 * 
 * @author liyg
 * 库存物流服务实现类
 * 2019-08-21
 *
 */
@Service("stockRecordService")
public class StockRecordServiceImpl implements StockRecordService{
	
	@Autowired
	private StockRecordMapper stockRecordMapper;

	/**
	 *获取库存记录列表
	 * @param map 参数
	 *key:carId      Integer 车辆id 非必填
	 *key:vehicleId  Integer 车型id 非必填
	 *key:type       Integer 出入库类型 非必填
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<StockRecordDto> getStockRecordList(Map<String, Object> map) {

		long total = stockRecordMapper.getPageCount(map);
		if( total <= 0 ) {
			return null;
		}
		
		Integer pageNum=ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize=ObjectCastUtil.castInteger(map.get("pageSize"));
		List<StockRecordBean> list=stockRecordMapper.getPage(map,pageNum,pageSize);
		List<StockRecordDto> stockRecordDtoList = Convert.toStockRecordDto(list);
		Paged<StockRecordDto> paged=new Paged<>(stockRecordDtoList,total,pageSize);
		
		return paged;
	}
	
	/**
	 * 根据车辆id获取库存历史流转记录
	 * @param carId
	 * @return
	 */
	@Override
	public List<StockRecordDto> getListByCarId(Integer carId) {
		List<StockRecordBean> list = stockRecordMapper.getListByCarId(carId);
		List<StockRecordDto> stockRecordDtoList = Convert.toStockRecordDto(list);
		return stockRecordDtoList;
	}

	/**
	 * 保存库存记录
	 * 
	 * @param createDto
	 * @return
	 */
	@Override
	public Boolean create(StockRecordCreateDto createDto) {
		
		StockRecordBean stockRecord=new StockRecordBean();
		BeanUtils.copyProperties(createDto, stockRecord);
		
		int num = stockRecordMapper.create(stockRecord);
		
		return num==1 ? true :false;
	}
	
	/**
	 * 获取收车回库数量
	 * @param outStockIds
	 * @param carIds
	 * @return
	 */
	@Override
	public Long getCount(List<Integer> outStockIds) {
		return stockRecordMapper.getCount(outStockIds);
	}
	
	/**
	 *获取库存记录列表(已回库)
	 * @param map 参数
	 *key:carId      Integer 车辆id 非必填
	 *key:stockIds   List 仓库ids
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<StockRecordDto> getReturnStockCarList(Map<String, Object> map) {

		long total = stockRecordMapper.getReturnStockCarListCount(map);
		if( total <= 0 ) {
			return null;
		}
		
		Integer pageNum=ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize=ObjectCastUtil.castInteger(map.get("pageSize"));
		List<StockRecordBean> list=stockRecordMapper.getReturnStockCarList(map,pageNum,pageSize);
		List<StockRecordDto> stockRecordDtoList = Convert.toStockRecordDto(list);
		Paged<StockRecordDto> paged=new Paged<>(stockRecordDtoList,total,pageSize);
		
		return paged;
	}
	
	/**
	 * 根据仓库ids 和 车辆id 查询车辆历史记录
	 * @param carId
	 * @param storeIds
	 * @return
	 */
	@Override
	public List<StockRecordDto> getListByCarId(Integer carId, List<Integer> stockIds) {
		List<StockRecordBean> list = stockRecordMapper.getListByCarIdAndStockIds(carId,stockIds);
		List<StockRecordDto> stockRecordDtoList = Convert.toStockRecordDto(list);
		return stockRecordDtoList;
	}
}
